{% extends 'admin/admin_base.html' %}

{% block title %}用户管理 - 后台管理系统{% endblock %}

{% block nav_users %}active{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>用户管理</h2>
    <a href="{{ url_for('admin.user_create') }}" class="btn btn-primary">
        创建用户
    </a>
</div>

<div class="card mb-4">
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-auto">
                <input type="text" class="form-control" name="search" placeholder="搜索用户名..." value="{{ search }}">
            </div>
            <div class="col-auto">
                <button type="submit" class="btn btn-secondary">搜索</button>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>年级</th>
                        <th>管理员</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                    <tr>
                        <td>{{ user.username }}</td>
                        <td>{{ user.email or '未设置' }}</td>
                        <td>{{ user.grade or '未设置' }}</td>
                        <td>
                            {% if user.is_admin %}
                            <span class="badge bg-primary">是</span>
                            {% else %}
                            <span class="badge bg-secondary">否</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="btn-group" role="group">
                                <a href="{{ url_for('admin.user_edit', user_id=user._id) }}" class="btn btn-sm btn-outline-primary">编辑</a>
                                <button type="button" class="btn btn-sm btn-outline-danger" 
                                        onclick="confirmDelete('{{ user._id }}', '{{ user.username }}')">删除</button>
                            </div>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="5" class="text-center">暂无用户数据</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        {% if total_pages > 1 %}
        <nav aria-label="Page navigation" class="mt-4">
            <ul class="pagination justify-content-center">
                <li class="page-item {% if current_page == 1 %}disabled{% endif %}">
                    <a class="page-link" href="{{ url_for('admin.user_list', page=current_page-1, search=search) }}">上一页</a>
                </li>
                
                {% for page in range(1, total_pages + 1) %}
                <li class="page-item {% if page == current_page %}active{% endif %}">
                    <a class="page-link" href="{{ url_for('admin.user_list', page=page, search=search) }}">{{ page }}</a>
                </li>
                {% endfor %}
                
                <li class="page-item {% if current_page == total_pages %}disabled{% endif %}">
                    <a class="page-link" href="{{ url_for('admin.user_list', page=current_page+1, search=search) }}">下一页</a>
                </li>
            </ul>
        </nav>
        {% endif %}
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除用户 "<span id="deleteUserName"></span>" 吗？此操作不可恢复！
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="post" style="display: inline;">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function confirmDelete(userId, username) {
    document.getElementById('deleteUserName').textContent = username;
    document.getElementById('deleteForm').action = "{{ url_for('admin.user_delete', user_id='') }}" + userId;
    
    var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
    deleteModal.show();
}
</script>
{% endblock %}
